/*
   This file is part of the 'Let's Do Stuff Together' project
   http://code.google.com/p/dostufftogether/

   Copyright 2010 Christoph Fuchs, Stefan Thaler

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.sti2.letsdostuff;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.sti2.letsdostuff.location.ClickableItemizedOverlay;
import org.sti2.letsdostuff.location.UnclickableItemizedOverlay;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * Detail view of a found activity. Shows name, description, time, where to meet, and category.
 * 
 * @author Christoph 'Gigi' Fuchs
 *
 */
public class ShowActivity extends MapActivity {

	private TextView descriptionLabel;
	private TextView titleLabel;
	private TextView categoryLabel;
	private TextView timeAndDateLabel;
	private MapView mapView;
	private List<Overlay> mapOverlays;
	private Drawable drawable;
	private ClickableItemizedOverlay itemizedOverlay;
	private Button buttonAttend;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity);
		
		titleLabel = (TextView) findViewById(R.id.label_activityName);
		descriptionLabel = (TextView) findViewById(R.id.label_activityDescription);
		timeAndDateLabel = (TextView) findViewById(R.id.label_activityTimeAndDate);
		categoryLabel = (TextView) findViewById(R.id.label_activityCategory);
		buttonAttend = (Button) findViewById(R.id.activity_button_attend);
		
		String title = getIntent().getExtras().getString(SearchResults.CLICKED_ACTIVITY_TITLE);
		String description = getIntent().getExtras().getString(SearchResults.CLICKED_ACTIVITY_DESCRIPTION);
		String category = null;
		
		long timeStart = getIntent().getExtras().getLong(SearchResults.CLICKED_ACTIVITY_TIME_START);
		long timeEnd = getIntent().getExtras().getLong(SearchResults.CLICKED_ACTIVITY_TIME_END);
		
		double latitude = getIntent().getExtras().getDouble(SearchResults.CLICKED_ACTIVITY_LATITUDE);
		double longitude = getIntent().getExtras().getDouble(SearchResults.CLICKED_ACTIVITY_LONGITUDE);
		
		titleLabel.setText(title);
		descriptionLabel.setText(description);
		timeAndDateLabel.setText(new SimpleDateFormat("'Starting at' HH:mm 'on' dd.MM.yyyy").format(new Date(timeStart)));
		
		
		/*
		 * Google Maps stuff
		 */
		mapView = (MapView) findViewById(R.id.activity_mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setStreetView(true);
		
		mapOverlays = mapView.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.marker);
		itemizedOverlay = new UnclickableItemizedOverlay(drawable);
		mapOverlays.add(itemizedOverlay);
		
		/*
		 * Create the marker and zoom to (animate to) the correct position
		 */
		GeoPoint p = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
		itemizedOverlay.addSingleOverlay(mapView, p);
		Log.i("ShowActivity", "Put marker at " + latitude + " " + longitude);
		
		MapController controller = mapView.getController();
		controller.animateTo(p);
		
		/*
		 * Attend button
		 */
		buttonAttend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast toast = Toast.makeText(getApplicationContext(), "You now attend " + titleLabel.getText(), Toast.LENGTH_SHORT );
				toast.show();
				
				Intent homeScreen = new Intent(getApplicationContext(), LetsDoStuff.class);
				startActivity(homeScreen);
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    // See which child activity is calling us back.
		Log.v("ShowActivity", "Got result code: " + resultCode);
	    switch (resultCode) {
	        case SearchResults.CLICKED_ACTIVITY:
	            // This is the standard resultCode that is sent back if the
	            // activity crashed or didn't doesn't supply an explicit result.
	            if (resultCode == RESULT_CANCELED){
	                Toast.makeText(this.getBaseContext(), "Something terrible happened. Please try again.", Toast.LENGTH_SHORT).show();
	            } else {
	            	Log.v("ShowActivity", "Retrieved activity details. Setting labels...");
	                titleLabel.setText(data.getStringExtra(SearchResults.CLICKED_ACTIVITY_TITLE));
	            }
	        default:
	            break;
	    }
	}

	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#isRouteDisplayed()
	 */
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
